package com.yjha.ai.demos.web.controller;



import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
@RequestMapping("/ai")
public class AIController {

    @Resource
    private ZhiPuAiChatModel chatModel;


    @GetMapping("/chat")
    String generate(@RequestParam(value = "message", defaultValue = "给我讲一个鬼故事") String message) {
        ChatResponse response = chatModel.call(
                new Prompt(
                        message,
                        ZhiPuAiChatOptions.builder()
                                .withModel("glm-4").build()
                ));
        return response.getResult().getOutput().getContent();
    }

    @GetMapping(value = "/stream" , produces = "text/event-stream;charset=utf-8")
    Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "给我讲一个鬼故事") String message) {
        var prompt = new Prompt(new UserMessage(message));
        return chatModel.stream(prompt);
    }
}
